//
//  LoginBox.m
//  MClient
//
//  Created by zhouyi on 10-12-8.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "LoginBox.h"

@implementation LoginBox
@synthesize userIdField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize cancelButton;

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate 
  cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
	self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle
			  otherButtonTitles:otherButtonTitles, nil];
	if (self) {
		UIColor *fontColor = [MCUserDefaults instance].skin_withFormButtonFontColor;
		
		UIImage *stretchableBg = [[MCUserDefaults instance].skin_withFormButtonBg stretchableImageWithLeftCapWidth:12 topCapHeight:15];
		UIImageView* background = [[UIImageView alloc] initWithFrame:self.bounds];
		background.image = stretchableBg;	
		background.autoresizingMask = nMCViewAutoresizingMask | UIViewAutoresizingFlexibleHeight;
		[self addSubview:background];
		[background release];
		[self sendSubviewToBack:background];
		
		UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 100, 30)];
		titleLabel.text = @"用户登录";
		titleLabel.textColor = fontColor;
		titleLabel.font = [UIFont boldSystemFontOfSize:20];
		titleLabel.backgroundColor = [UIColor clearColor];
		[self addSubview:titleLabel];
		[titleLabel release];
		
		UILabel *userIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 45, 50, 30)];
		userIdLabel.text = @"用户";
		userIdLabel.textColor = fontColor;
		userIdLabel.font = [UIFont boldSystemFontOfSize:18];
		userIdLabel.backgroundColor = [UIColor clearColor];
		[self addSubview:userIdLabel];
		[userIdLabel release];
		
		userIdField = [[UITextField alloc] initWithFrame:CGRectMake(70, 45, 190, 30)];
		userIdField.backgroundColor = [UIColor clearColor];
		userIdField.borderStyle = UITextBorderStyleRoundedRect;
		userIdField.placeholder = @"请输入用户名";
		[userIdField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
		[self addSubview:userIdField];
		
		UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 85, 50, 30)];
		passwordLabel.text = @"密码";
		passwordLabel.textColor = fontColor;
		passwordLabel.font = [UIFont boldSystemFontOfSize:18];
		passwordLabel.backgroundColor = [UIColor clearColor];
		[self addSubview:passwordLabel];	
		[passwordLabel release];
		
		passwordField = [[UITextField alloc] initWithFrame:CGRectMake(70, 85, 190, 30)];
		passwordField.backgroundColor = [UIColor clearColor];
		passwordField.borderStyle = UITextBorderStyleRoundedRect;
		passwordField.placeholder = @"请输入密码";
		passwordField.secureTextEntry = YES;
		[passwordField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
		[self addSubview:passwordField];
		
		loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		loginButton.frame = CGRectMake(40, 130, 80, 30);
		loginButton.backgroundColor = [UIColor clearColor];
		UIImage *buttonBg = [MCUserDefaults instance].skin_withFormButtonBg;
		UIImage *stretchableButtonBg = [buttonBg stretchableImageWithLeftCapWidth:12 topCapHeight:5];
		[loginButton setBackgroundImage:stretchableButtonBg forState:UIControlStateNormal];
		[loginButton setTitle:@"登录" forState:UIControlStateNormal];
		[loginButton setTitleColor:fontColor forState:UIControlStateNormal];
		[loginButton addTarget:self action:@selector(selectAction) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:loginButton];
		
		cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		cancelButton.frame = CGRectMake(165, 130, 80, 30);
		cancelButton.backgroundColor = [UIColor clearColor];
		[cancelButton setBackgroundImage:stretchableButtonBg forState:UIControlStateNormal];
		[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
		[cancelButton setTitleColor:fontColor forState:UIControlStateNormal];
		[cancelButton addTarget:self action:@selector(selectCancelAction) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:cancelButton];
	}
	return self;
}

- (void)dealloc {
	[userIdField release];
	[passwordField release];
	[loginButton release];
	[cancelButton release];
    [super dealloc];
}

- (void)hideKeyboard {
	[userIdField resignFirstResponder];
	[passwordField resignFirstResponder];
	[self resignFirstResponder];
}

- (void)selectCancelAction {
	[self hideKeyboard];
	[self dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)selectAction {
	[self hideKeyboard];
	if (nil == userIdField.text || [@"" isEqualToString:userIdField.text]) {
		[[MCErrorManagement instance] doError:@"99" returnMessage:@"用户名不能为空!"];
		return;
	}
	if (nil == passwordField.text || [@"" isEqualToString:passwordField.text]) {
		[[MCErrorManagement instance] doError:@"99" returnMessage:@"密码不能为空!"];
		return;
	}
	NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:userIdField.text, @"username", passwordField.text, @"password",nil];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"username:password" object:dictionary];
	[self selectCancelAction];
}

- (void) textFieldDone:(id)sender
{
	[self resignFirstResponder];
	[sender resignFirstResponder];
}
@end

